package com.hy.controller.tools;

import com.hy.domain.dto.R;
import com.hy.service.FileInfoService;
import com.hy.util.SHA1Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileInfoService fileInfoService;

    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
        // 判断文件以否已经存在
        return fileInfoService.saveFile(file);
    }

    @GetMapping("/download/{fileName}")
    public R download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        // 判断文件以否已经存在
        return fileInfoService.getFileByName(fileName, response);
    }
}
